package cn.cgy.fate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ChoiceNumberForMoney {

    public static final int RED_NUM = 6;

    public static final int BLUE_NUM = 1;

    public static final int REA_MAX = 33;

    public static final int REA_MIN = 1;

    public static final int BLUE_MAX = 16;

    public static final int BLUE_MIN = 1;

    public static final int TICKET_GROUP = 5;

    public static final String SEPARATION = ",";

    public static String getTickByFate(int index) {

        List<Integer> redNumber = getRedNumber(null);

        List<Integer> blueNumber = getBlueNumber(null);

        return "第" + index + "组：【红球】" + list2Str(redNumber) + "【蓝球】" + list2Str(blueNumber) + "<br>";
    }

    /**
     * 获取红色球【1~33】
     *
     * @return
     */
    public static List<Integer> getRedNumber(List<Integer> rawList) {

        if (!checkIsRepeatGroup(rawList, true)) {

            return rawList;
        }

        List<Integer> redNumberList = new ArrayList<>();

        for (int i = 0; i < RED_NUM; i++) {

            redNumberList.add(getRandom(REA_MAX, REA_MIN));
        }

        return getRedNumber(redNumberList);
    }

    /**
     * 获取蓝色球【1~16】
     *
     * @return
     */
    public static List<Integer> getBlueNumber(List<Integer> rawList) {

        if (!checkIsRepeatGroup(rawList, false)) {

            return rawList;
        }

        List<Integer> blueNumberList = new ArrayList<>();

        for (int i = 0; i < BLUE_NUM; i++) {

            blueNumberList.add(getRandom(BLUE_MAX, BLUE_MIN));
        }

        return getBlueNumber(blueNumberList);
    }

    /**
     * 获取指定范围随机数
     *
     * @param max
     * @param min
     * @return
     */
    public static int getRandom(int max, int min) {

        return (int) (Math.random() * (max - min) + min);
    }

    /**
     * 集合转特定分割符字符串
     *
     * @param list
     * @return
     */
    public static String list2Str(List<Integer> list) {

        StringBuilder stringBuilder = new StringBuilder();

        if (list == null || list.isEmpty()) {

            return null;
        }

        list = list.stream().sorted().collect(Collectors.toList());

        for (Object o : list) {

            String numFormat = o.toString().length() == 1 ? "0" + o.toString() : o.toString();

            stringBuilder.append(numFormat + SEPARATION);
        }

        String format = stringBuilder.toString();

        return format.substring(0, format.length() - 1);
    }

    /**
     * 检查号码组是否存在重复号码
     *
     * @param numberGroup
     * @param isRed
     * @return
     */
    public static boolean checkIsRepeatGroup(List<Integer> numberGroup, boolean isRed) {

        boolean isRepeat = true;

        // 没有数据视为重复
        if (numberGroup == null || numberGroup.isEmpty()) {

            return true;
        }

        int size = numberGroup.stream().distinct().collect(Collectors.toList()).size();

        if (isRed && size == RED_NUM) {

            isRepeat = false;
        }

        if (!isRed && size == BLUE_NUM) {

            isRepeat = false;
        }

        return isRepeat;
    }
}
